<?php

namespace App\Miya\Middlewares;

use Closure;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;

class CopyrightSign
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $html = Cache::remember('copyright_sign_html', 24 * 60 * 60, function () {
            $tql = <<<TQL
<!--
本程序由以下企业共同研发,如未授权禁止使用:
%s

本程序已申请软件著作权,违法必究:
%s

详情请访问一下网址查看:%s
-->
TQL;
            $companies = implode('、', config('copyright.authorization_company'));
            $serial = array_column(config('copyright.serial'), 'name');

            $html = sprintf(
                $tql,
                $companies,
                implode('、', $serial),
                request()->server('HTTP_HOST') . '/_copyright'
            );
            return $html;
        });

        $content = $response->content();
        $response->setContent($content . $html);
        return $response;
    }
}
